<%-- 
    Document   : newEvent
    Created on : May 12, 2011, 9:24:02 PM
    Author     : enriquer
--%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page contentType="text/html" pageEncoding="UTF-8"%>

<script type="text/javascript">
    dojo.require("dijit.layout.ContentPane");
    dojo.require("dijit.form.Button");
    dojo.require("dijit.form.Form");
    dojo.require("dijit.form.ValidationTextBox");
    dojo.require("dijit.form.SimpleTextarea");
    dojo.require("dijit.form.DateTextBox");
    dojo.require("dijit.form.CheckBox");
    dojo.require("dijit.TitlePane");
    dojo.require("dijit.layout.TabContainer");
    dojo.require("dijit.form.TimeTextBox");
    dojo.require("dijit.form.NumberSpinner");
    dojo.require("dojo.data.ItemFileReadStore");

    var totalItems = 0; //How many total items should we expect.
    var currentStart = 0; //Current index into the pages.
    var currentCount = 10; //Current size of the page.
    var request=null;
                
               
    //Callback for if the lookup fails.
    function fetchFailed(error, request) {
        alert("lookup failed.");
    }

    //Button event to page forward.
    function nextPage() {
        //If we haven't hit the end of the pages yet, allow for requesting another.
        if ((currentStart + currentCount) < totalItems) {
            request.start += currentCount;
            request = store.fetch(request);
        }
    }

    //Button event to page back;
    function previousPage() {
        //If we haven't hit the beginning of the pages yet, allow for another shift backwards.
        if (currentStart > 0) {
            request.start -= currentCount;
            request = store.fetch(request);
        }
    }
                
    dojo.addOnLoad(function() {
                
        loadMenu();
        function clearOldList(size, request) {
            var list = dojo.byId("listEvents");
            if (list) {
                while (list.firstChild) {
                    list.removeChild(list.firstChild);
                }
            }
            //Save off the total size. We need it to determine when to ignore the buttons.
            totalItems = size;
        }

        //Callback for processing a returned list of items.
        function gotItems(items, request) {
            //Save off the current page info being displayed.
            currentStart = request.start;
            currentCount = request.count;
            var list = dojo.byId("listEvents");
            if (list) {
                var i;
                
                for (i = 0; i < items.length; i++) {
                    var item = items[i];
                    var name="<div><h2><a href=''>"+store.getValue(item, 'name')+"</a></h2></div>";
                    var description="<div><h2>"+store.getValue(item, 'description')+"</h2></div>";
                    var date="<div>"+store.getValue(item, 'startDate')+" - "+store.getValue(item, 'endDate')+"</div>";
                    var eventElement=document.createElement("div");
                    eventElement.innerHTML=name+description+date;
                    list.appendChild(eventElement);
                    
                }
                
            }
        }
        
        request = store.fetch({
            onBegin: clearOldList,
            onComplete: gotItems,
            onError: fetchFailed,
            start: currentStart,
            count: currentCount
        });
       
    });
</script>

<div dojoType="dojo.data.ItemFileReadStore" jsId="store" url="lsEvents.htm" 
     urlPreventCache="true" clearOnClose="true">
</div>
<div style="width: 600px; height: 350px;">
    <div id="divMessage" align="center"></div>

    <div dojoType="dijit.form.Button" jsId="back" onclick="javascript:previousPage();">
        Anterior
    </div>
    <div dojoType="dijit.form.Button" jsId="forward" onclick="javascript:nextPage();">
        Siguiente
    </div>
    <br>
    <span id="listEvents">
    </span>
</div>
